package com.itany.controller;


import com.github.pagehelper.PageInfo;
import com.itany.constant.ResponseConstant;
import com.itany.exception.RequestParamsException;
import com.itany.exception.RoleExistsExceptions;
import com.itany.exception.UsingRoleException;
import com.itany.pojo.Role;
import com.itany.service.RoleService;
import com.itany.vo.ActionResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    RoleService roleService;


    /**
     * 添加角色
     * @param name 角色名称
     * @param permissions 角色权限
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public ActionResult addRole(String name, @RequestParam(value = "permissions[]",required = false)Integer[] permissions){
        ActionResult result = new ActionResult();
        try {
            roleService.addRole(name,permissions);
            result.setMsg("添加成功");
            result.setStatus(ResponseConstant.RESPONSE_SUCCESS);
        } catch (RequestParamsException e) {
            e.printStackTrace();
            result.setMsg(e.getMessage());
            result.setStatus(ResponseConstant.REQUEST_PARAMETER_ERROR);
        }catch (RoleExistsExceptions e){
            result.setMsg(e.getMessage());
            result.setStatus(ResponseConstant.RESPONSE_FAIL);
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("服务器内部异常");
            result.setStatus(ResponseConstant.RESPONSE_FAIL);
        }
        return result;
    }

    /**
     * 查找所有角色
     * @param page 页码数
     * @param rows 每页数据条数
     * @return 分页角色信息
     */
    @RequestMapping("/findAll")
    @ResponseBody
    public Map<String,Object> findAll(@RequestParam(defaultValue="1")Integer page,
                                      @RequestParam(defaultValue="10")Integer rows,
                                      String rolenameKey){
        Map<String,Object> map = new HashMap<>();
        PageInfo<Role> info = null;
        try {
            info = roleService.findAll(page, rows,rolenameKey);
        } catch (RequestParamsException e) {
            e.printStackTrace();
        }
        map.put("total", info.getTotal());
        map.put("rows", info.getList());
        return map;
    }


    /**
     * 生成角色树状
     */
    @RequestMapping("/findRoletree")
    @ResponseBody
    public List<Map<String, Object>> findRoletree() {
        List<Map<String, Object>> tree = new ArrayList<>();
        List<Role> roles = roleService.findAllNoPages();
        for (Role role : roles) {
            Map<String, Object> node = new HashMap<>();
            node.put("id", role.getId());
            node.put("text", role.getName());
            tree.add(node);
        }
        return tree;
    }


    /**
     * 修改角色信息
     * @param id 角色id
     * @param name 角色名
     * @param permissions 角色权限
     * @return
     */
    @RequestMapping("/modify")
    @ResponseBody
    public ActionResult modifyRole(String id, String name, @RequestParam(value = "permissions[]",required = false)Integer[] permissions){
        ActionResult result = new ActionResult();
        try {
            roleService.modifyRole(id, name, permissions);
            result.setMsg("修改成功");
            result.setStatus(ResponseConstant.RESPONSE_SUCCESS);
        } catch (RequestParamsException e) {
            result.setMsg(e.getMessage());
            result.setStatus(ResponseConstant.REQUEST_PARAMETER_ERROR);
        }catch (RoleExistsExceptions e){
            result.setMsg(e.getMessage());
            result.setStatus(ResponseConstant.RESPONSE_FAIL);
        }catch (Exception e){
            result.setMsg("服务器内部异常");
            result.setStatus(ResponseConstant.SERVER_ERROR);
        }
        return result;
    }


    /**
     * 删除角色信息
     * @param id 角色id
     */
    @RequestMapping("/remove")
    @ResponseBody
    public ActionResult removeRole(String id){
        ActionResult result = new ActionResult();
        try {
            roleService.removeRole(id);
            result.setMsg("删除成功");
            result.setStatus(ResponseConstant.RESPONSE_SUCCESS);
        } catch (RequestParamsException e) {
            result.setMsg(e.getMessage());
            result.setStatus(ResponseConstant.REQUEST_PARAMETER_ERROR);
        }catch (UsingRoleException e){
            result.setMsg(e.getMessage());
            result.setStatus(ResponseConstant.RESPONSE_FAIL);
        } catch (Exception e){
            e.printStackTrace();
            result.setMsg("服务器内部异常");
            result.setStatus(ResponseConstant.SERVER_ERROR);
        }
        return result;
    }
}
